<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="formData.name" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="封面" prop="cover">
                    <el-input v-model="formData.cover" placeholder="请输入封面" />
                </el-form-item>
                <el-form-item label="地点" prop="address">
                    <el-input v-model="formData.address" placeholder="请输入地点" />
                </el-form-item>
                <el-form-item label="比赛时间" prop="playTime">
                    <el-input v-model="formData.playTime" placeholder="请输入比赛时间" />
                </el-form-item>
                <el-form-item label="比赛方式" prop="playType">
                    <el-select class="flex-1" v-model="formData.playType" placeholder="请选择比赛方式">
                        <el-option label="男单打" value="1" />
                        <el-option label="女单打" value="2" />
                        <el-option label="男双打" value="3" />
                        <el-option label="女双打" value="4" />
                        <el-option label="男女混打" value="5" />
                    </el-select>
                </el-form-item>
                <el-form-item label="赛程" prop="schedule">
                    <el-select class="flex-1" v-model="formData.playType" placeholder="请选择赛程">
                      <el-option label="初赛" value="1" />
                      <el-option label="半决赛" value="2" />
                      <el-option label="总决赛" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="" prop="status">
                    <el-radio-group v-model="formData.status" placeholder="请选择">
                        <el-radio label="0">请选择字典生成</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="组织方" prop="orgParty">
                    <el-input v-model="formData.orgParty" placeholder="请输入组织方" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  competitionEdit, competitionAdd, competitionDetail } from '@/api/competition/competition'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑赛事列' : '新增赛事列'
})

const formData = reactive({
    id: '',
    name: '',
    cover: '',
    address: '',
    playTime: '',
    playType: '',
    schedule: '',
    status: '',
    orgParty: '',
    orgId: '',
    createId: '',
    updateId: '',
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    name: [
        {
            required: true,
            message: '请输入名称',
            trigger: ['blur']
        }
    ],
    cover: [
        {
            required: true,
            message: '请输入封面',
            trigger: ['blur']
        }
    ],
    address: [
        {
            required: true,
            message: '请输入地点',
            trigger: ['blur']
        }
    ],
    playTime: [
        {
            required: true,
            message: '请输入比赛时间',
            trigger: ['blur']
        }
    ],
    playType: [
        {
            required: true,
            message: '请选择比赛方式：1:男单打，2:女单打，3:男双打，4:女双打，5:男女混打',
            trigger: ['blur']
        }
    ],
    schedule: [
        {
            required: true,
            message: '请输入赛程：初赛，半决赛，决赛',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择',
            trigger: ['blur']
        }
    ],
    orgParty: [
        {
            required: true,
            message: '请输入组织方',
            trigger: ['blur']
        }
    ],
    orgId: [
        {
            required: true,
            message: '请输入组织id',
            trigger: ['blur']
        }
    ],
    createId: [
        {
            required: true,
            message: '请输入创建id',
            trigger: ['blur']
        }
    ],
    updateId: [
        {
            required: true,
            message: '请输入更新id',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await competitionEdit(data) : await competitionAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await competitionDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
